package com.qqtech.qquan.mall.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.qqtech.core.common.util.BeanUtil;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.dao.BaseDaoImpl;
import com.qqtech.qquan.mall.model.MallShopCategory;
import com.qqtech.qquan.mall.model.MallShopCategoryVo;

@Repository
public class MallShopCategoryDaoImpl extends BaseDaoImpl<MallShopCategory> implements MallShopCategoryDao {
	public static final Logger log = LoggerFactory.getLogger(MallShopCategoryDaoImpl.class);
	
	public MallShopCategoryVo getByCategoryId(java.lang.String categoryId) {
		return sqlSessionTemplate.selectOne(getSqlName("selectByCategoryId"), categoryId);
	}

	@Override
	public Integer getMaxOidx(MallShopCategory mallShopCategory) {
		return (Integer) sqlSessionTemplate.selectOne(getSqlName("getMaxOidx"),mallShopCategory);
	}

	@Override
	public List<MallShopCategoryVo> findAllParent(String categoryId) {
		if (categoryId != null) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("categoryId", categoryId);
			return sqlSessionTemplate.selectList(getSqlName("selectAllParent"), map);
		}
		return null;
	}

	@Override
	public int updateByCategoryId(MallShopCategory entity) {
		if (StringUtil.isBlank(entity.getCategoryId())) {
			return 0;
		}
		return sqlSessionTemplate.update(getSqlName("updateByCategoryId"), entity);
	}

	@Override
	public Page<MallShopCategoryVo> queryPageListAdmin(MallShopCategory query,
			Pageable pageable) {
		List<MallShopCategoryVo> contentList = sqlSessionTemplate.selectList(getSqlName("selectAdmin"), getParams(query, pageable));
		return new PageImpl<MallShopCategoryVo>(contentList, pageable, this.queryAdminCount(query));
	}

	@Override
	public int queryAdminCount(MallShopCategory query) {
		return sqlSessionTemplate.selectOne(getSqlName("selectAdminCount"), BeanUtil.toMap(query));
	}

	@Override
	public List<MallShopCategoryVo> findByShop(Integer communityId, Integer style) {
		if (communityId != null && style != null) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("communityId", communityId);
			map.put("style", style);
			return sqlSessionTemplate.selectList(getSqlName("selectByShop"), map);
		}
		return null;
	}
}
